package com.gaofei.user.controller;


import com.bw.itext.pdf.service.PdfService;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.gaofei.user.domain.House;
import com.gaofei.user.domain.User;
import com.gaofei.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaofei
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    IUserService userService;

    @Autowired
    private PdfService pdfService;


    @Autowired
    private FileService fileService;


    /**
     * 文件分块上传
     * @param chunk
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("postFileUpload")
    public Object postFileUpload(@ModelAttribute Chunk chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            TimeUnit.SECONDS.sleep(2);
            FileResult fileResult = fileService.mergeFile(chunk);
            return fileResult;
        }
        log.info("文件上传成功");
        return "success!";
    }

    @RequestMapping("list")
    public Object list(){
        List<User> list = userService.list(null);
        return list;
    }

    @RequestMapping("download")
    public String download(HttpServletResponse resp) throws IOException {
        List<User> list = userService.list(null);
//        生成pdf
        pdfService.download("user.sign.pdf",resp);
//

        return "success!!!";
    }

    @RequestMapping("preview")
    public String preview(HttpServletResponse resp) throws IOException {
        List<User> list = userService.list(null);
//        预览pdf
        pdfService.preview("user.sign.pdf",resp);
//

        return "success!!!";
    }
    @RequestMapping("pdf1")
    public String pdf1() throws IOException {
        List<User> list = userService.list(null);
//        生成pdf
        pdfService.export(list,"user.pdf","导出demo",User.class);
//        电子签章
        pdfService.sign("user.pdf","user.sign.pdf");

        return "success!!!";
    }

    @RequestMapping("pdf")
    public String pdf() throws IOException {
        List<House> list = new ArrayList<>();
        House house = new House();
        house.setId(1);
        house.setAddress("北京市");
        house.setCommunity("环境优美");
        list.add(house);
        House house1 = new House();
        house1.setId(2);
        house1.setAddress("天津市");
        house.setCommunity("环境优美");
        list.add(house);
//        生成pdf
        pdfService.export(list,"house.pdf","导出demo",House.class);
//        电子签章
        pdfService.sign("house.pdf","house.sign.pdf");

        return "success!!!";
    }
}

